package com.example.sunmiv1.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.CollectingSilverActivity;
import com.example.sunmiv1.entity.GoodBean;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.sunmiv1.activity.CollectingSilverActivity.infoBean;

public class CollectingSilverGoodsAdapter extends BaseAdapter {

    private Context context;
    private List<GoodBean.DataBean.GoodsBean> list;
    ViewHolder viewHolder;
    private Util util;
    private ACache aCache;

    public CollectingSilverGoodsAdapter(Context context, List<GoodBean.DataBean.GoodsBean> list) {
        this.context = context;
        this.list = list;
        aCache = ACache.get(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.collecting_silver_goods_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GoodBean.DataBean.GoodsBean goodsBean = list.get(position);

        RoundedCorners roundedCorners = new RoundedCorners(6);
        //通過RequestOptions擴展功能,override:採樣率,因爲ImageView就這麼大,可以壓縮圖片,降低內存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        options.placeholder(R.mipmap.collecting_silvergoods_error);//佔位圖  .error(R.mipmap.error_food)//如果是錯誤的情況
        Glide.with(context)
                .load(goodsBean.getGoods_img())
                .apply(options)
                .into(viewHolder.collectingSilverGoodsAdapterImg);
        viewHolder.collectingSilverGoodsAdapterName.setText(goodsBean.getGoods_name());
        if ("1".equals(goodsBean.getPricing_method())) {
//            viewHolder.collectingSilverGoodsAdapterNum.setText(goodsBean.getAgoods_count() == null ? "" : String.valueOf(goodsBean.getAgoods_count()));
            viewHolder.collectingSilverGoodsAdapterPrice.setText("$" + goodsBean.getRetail_price());
        } else {
//            viewHolder.collectingSilverGoodsAdapterNum.setText(goodsBean.getAgoods_count() == null ? "" : String.valueOf(goodsBean.getAgoods_count()));
            viewHolder.collectingSilverGoodsAdapterPrice.setText("$" + goodsBean.getRetail_price() + "/" + goodsBean.getCompany());
        }

        if ("2".equals(goodsBean.getStore_mem_status()) && "2".equals(goodsBean.getPlus_mem_status())) {
            viewHolder.llVipPrice.setVisibility(View.GONE);
        } else {
            if (CollectingSilverActivity.isVip == 1) {
                if ("3".equals(infoBean.getPlus_type()) && "1".equals(goodsBean.getStore_mem_status()) &&
                        goodsBean.getStore_mem_price() != null && !TextUtils.isEmpty(goodsBean.getStore_mem_price())) {
                    viewHolder.llVipPrice.setVisibility(View.VISIBLE);
                    if ("1".equals(aCache.getAsString("marketing_methods"))) {
                        viewHolder.tvVipPrice.setText("" + goodsBean.getStore_mem_price());
                    } else {
                        viewHolder.tvVipPrice.setText("" + goodsBean.getStore_mem_price() + goodsBean.getStore_mem_integralInfo());
                    }
                    viewHolder.ivVipPrice.setImageResource(R.mipmap.vip);
                } else if ("2".equals(infoBean.getPlus_type()) && "1".equals(goodsBean.getPlus_mem_status()) && goodsBean.getPlus_price() != null && !TextUtils.isEmpty(goodsBean.getPlus_price())) {
                    viewHolder.llVipPrice.setVisibility(View.VISIBLE);
                    if ("1".equals(aCache.getAsString("marketing_methods"))) {
                        viewHolder.tvVipPrice.setText("" + goodsBean.getPlus_price());
                    } else {
                        viewHolder.tvVipPrice.setText("" + goodsBean.getPlus_price() + goodsBean.getPlus_mem_integralInfo());
                    }
                    viewHolder.ivVipPrice.setImageResource(R.mipmap.plus);
                } else {
                    viewHolder.llVipPrice.setVisibility(View.GONE);
                }
            } else {
                viewHolder.llVipPrice.setVisibility(View.GONE);
            }
        }

        final int index = position;
        final RelativeLayout jia = viewHolder.collectingSilverGoodsAdapterReduce;
        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.util(index, 1);
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.util(index,2);
            }
        });
//        final LinearLayout jian = viewHolder.collectingSilverGoodsAdapterPlus;
//        jian.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (list.get(index).getAgoods_count() > 0) {
//                    util.util(index, 2);
//                }
//            }
//        });
        return convertView;
    }

    public interface Util {
        void util(int index, int type);//前一個參數：點擊的第幾個，第二個參數：點擊的哪個按鈕
    }

    public void onclick(Util util) {
        this.util = util;
    }

    static class ViewHolder {
        @BindView(R.id.collectingSilver_goodsAdapter_img)
        ImageView collectingSilverGoodsAdapterImg;
        @BindView(R.id.collectingSilver_goodsAdapter_name)
        TextView collectingSilverGoodsAdapterName;
        @BindView(R.id.collectingSilver_goodsAdapter_price)
        TextView collectingSilverGoodsAdapterPrice;
        @BindView(R.id.iv_vip_price)
        ImageView ivVipPrice;
        @BindView(R.id.tv_vip_price)
        TextView tvVipPrice;
        @BindView(R.id.ll_vip_price)
        LinearLayout llVipPrice;
        @BindView(R.id.item_view)
        LinearLayout itemView;
//        @BindView(R.id.collectingSilver_goodsAdapter_plus)
//        LinearLayout collectingSilverGoodsAdapterPlus;
//        @BindView(R.id.collectingSilver_goodsAdapter_num)
//        TextView collectingSilverGoodsAdapterNum;
        @BindView(R.id.collectingSilver_goodsAdapter_reduce)
        RelativeLayout collectingSilverGoodsAdapterReduce;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
